package com.amat0urheu.controller;

import com.amat0urheu.dao.ExperimentsMapper;
import com.amat0urheu.dao.UploadFileDao;
import com.amat0urheu.pojo.Experiment;
import com.amat0urheu.pojo.ReturnRes;
import com.amat0urheu.service.ExperimentService;
import com.amat0urheu.service.UploadService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.xml.ws.Action;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardCopyOption;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
@RestController
    public class ExperimentFileController {
    @Value("${file.path}")
    private String savePath;
        @GetMapping("/admin/downloadAllExperiments1")
        public ResponseEntity<byte[]> downloadAllExperiments() {

            try {
                // 1. 创建临时目录
                Path tempDirectory = Files.createTempDirectory("experiments");

                // 2. 遍历每个同学的实验文件，按实验分类存放到临时目录中的相应文件夹下
                List<StudentExperiment> experiments = getStudentExperiments(); // 你的方法获取实验数据的逻辑
                for (StudentExperiment experiment : experiments) {
                    Path experimentDirectory = tempDirectory.resolve(experiment.getName());
                    Files.createDirectories(experimentDirectory);
                    copyFilesToDirectory(experiment.getFiles(), experimentDirectory);
                }

                // 3. 压缩文件夹
                Path zipFilePath = tempDirectory.resolve("experiments.zip");
                zipDirectory(tempDirectory, zipFilePath);

                // 4. 返回给前端
                byte[] zipFileBytes = Files.readAllBytes(zipFilePath);
                HttpHeaders headers = new HttpHeaders();
                headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
                headers.setContentDispositionFormData("attachment", "experiments.zip");
                return new ResponseEntity<>(zipFileBytes, headers, HttpStatus.OK);

            } catch (IOException e) {
                e.printStackTrace();
                return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR);
            }
        }

    private void copyFilesToDirectory(List<File> files, Path destinationDirectory) throws IOException {
        for (File file : files) {
            Path destinationFile = destinationDirectory.resolve(file.getName());
            Files.copy(file.toPath(), destinationFile, StandardCopyOption.REPLACE_EXISTING);
        }
    }

    private void zipDirectory(Path sourceDirectory, Path zipFilePath) throws IOException {
        try (ZipOutputStream zipOutputStream = new ZipOutputStream(new FileOutputStream(zipFilePath.toFile()))) {
            Files.walk(sourceDirectory)
                    .filter(path -> !Files.isDirectory(path))
                    .forEach(path -> {
                        try {
                            String relativePath = sourceDirectory.relativize(path).toString();
                            zipOutputStream.putNextEntry(new ZipEntry(relativePath));
                            Files.copy(path, zipOutputStream);
                            zipOutputStream.closeEntry();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    });
        }
    }
@Autowired
    UploadService uploadService;
    @Autowired
    ExperimentsMapper experimentsMapper;
    @Resource
    private UploadFileDao uploadFileDao;
    // 这是一个示例方法，用于获取学生实验数据
    private List<StudentExperiment> getStudentExperiments() {
        // 实现获取学生实验数据的逻辑
        // 返回每个学生的实验数据，包括实验名称和文件列表
        // 示例数据仅供参考
        List<StudentExperiment> experiments = new ArrayList<>();
        List<Experiment> allExperiments = experimentsMapper.getAllExperiments();

        for (int i = 0; i < allExperiments.size(); i++) {
            List<File> fileList=new ArrayList<>();
            List<String> filePathsList = uploadFileDao.getFilePaths(allExperiments.get(i).getEname());
            if( filePathsList.size()!=0){
                for (int j = 0; j < filePathsList.size(); j++) {
                //添加文件
                fileList.add(new File(savePath+filePathsList.get(j)));
            }}
            experiments.add(new StudentExperiment(allExperiments.get(i).getEname(),  fileList));
        }

        return experiments;
    }

    // 学生实验类，包含实验名称和文件列表
    private  class StudentExperiment {
        private String name;
        private List<File> files;

        public StudentExperiment(String name, List<File> files) {
            this.name = name;
            this.files = files;
        }

        public String getName() {
            return name;
        }

        public List<File> getFiles() {
            return files;
        }
    }
}